package cn.nexttec.noteplayer.common

import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.io.IOException

class AppendFileWriter(file: File) {

    private val writer: BufferedWriter

    init {
        // 创建父目录
        if (!file.parentFile.exists()) {
            file.parentFile.mkdirs()
        }
        writer = BufferedWriter(FileWriter(file, true)) // append = true
    }

    @Synchronized
    fun writeLine(text: String) {
        try {
            writer.write(text)
            writer.newLine()
            writer.flush()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    fun close() {
        try {
            writer.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}
